package com.zsix.system.strategy.copyFor;

import com.zsix.system.mapper.taskAssigneeMapper.CopyForMapper;
import com.zsix.common.dto.controllerParams.CCParams;
import com.zsix.common.entity.taskAssignee.CopyForEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
public class CCByUsersStrategy implements CCStrategy {
    @Autowired
    private CopyForMapper copyForMapper;

    /**
     * 设置抄送人员
     * @param ccParams
     * @return
     */
    @Override
    public String setCCData(CCParams ccParams){
        try {
            List<CCParams.CCData> ccDataList = ccParams.getData();
            List<CopyForEntity> copyForEntities =
                    ccDataList.stream().map(cc->{
                        CopyForEntity copyForEntity = new CopyForEntity();
                        copyForEntity.setMypk(UUID.randomUUID().toString());
                        copyForEntity.setTargetId(cc.getUsername());
                        copyForEntity.setCcWay(ccParams.getCcWay());
                        copyForEntity.setAutoCCWay(ccParams.getAutoCCWay());
                        copyForEntity.setTask_def_key(ccParams.getTask_key());
                        copyForEntity.setProc_inst_id(ccParams.getProc_inst_id());
                        return copyForEntity;
                    }).collect(Collectors.toList());
            copyForMapper.deleteCCDataByTaskKey(ccParams.getTask_key());
            copyForMapper.insertCCData(copyForEntities);
            return "设置成功";
        }
        catch (Exception ex){
            return "设置失败";
        }
    }

    /**
     * 获取抄送设置的人员信息
     * @param ccParams
     * @return
     */
    @Override
    public List<HashMap<String,Object>> getSelectData(CCParams ccParams){
        List<HashMap<String,Object>> hashMapList=
                Optional.ofNullable(copyForMapper.selectByUserData(ccParams))
                        .map(list->list.stream().map(it->{
                            HashMap<String,Object> hashMap=new HashMap<>();
                            hashMap.put("id",it.getId());
                            hashMap.put("username",it.getUsername());
                            hashMap.put("name",it.getName());
                            hashMap.put("departname",it.getDepartname());
                            return hashMap;
                        }).collect(Collectors.toList())).orElse(new ArrayList<>());

        return hashMapList;
    }
    /**
     * 服务类型
     * @return
     */
    @Override
    public String getType(){
        return "byUsers";
    }
}
